package com.yx.cloud.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;

import com.yx.cloud.constant.Constants;
import com.iot.common.utils.LogUtils;

import java.util.Locale;

/**
 * 语言切换
 */
public class LanguageUtils {

    private static final String TAG = "LanguageUtils";

    /**
     * 更改应用语言
     *
     * @param locale
     */
    public static void changeAppLanguage(Context context, Locale locale) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        Configuration config = context.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            Log.i(TAG, "1--aaaaaaaaaa--> ");
            config.setLocale(locale);
        } else {
//            Log.i(TAG, "1--bbbbbbbbbbb--> ");
            config.locale = locale;
        }
//        Log.i(TAG, "1--cccccccccccccccccc--> ");
        context.getResources().updateConfiguration(config, metrics);
    }

    /**
     * 获取系统当前语音
     *
     * @return
     */
    public static String getSystemLanguage(Context context) {
//        Locale locale;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            locale = context.getResources().getConfiguration().getLocales().get(0);
//        } else {
//            locale = context.getResources().getConfiguration().locale;
//        }
        String language = Locale.getDefault().getLanguage();
        String country = Locale.getDefault().getCountry();
        String lang = language + "-" + country; //华为手机有毒 获取的全是cn*/;
        LogUtils.i("获取系统当前语言  :" + lang);
        return lang;
    }

    /**
     * 获取系统当前语音
     *
     * @return
     */
    public static String getProductSuffix(Context context) {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = context.getResources().getConfiguration().getLocales().get(0);
        } else {
            locale = context.getResources().getConfiguration().locale;
        }
        String lang = locale.getLanguage(); //华为手机有毒 获取的全是cn*/;
//        String counery = locale.getCountry();
//        LogUtils.i("获取系统当前语言 counery :" + counery);
        return lang;
    }

    private static String mLanguage;

    /**
     * 设置程序语言
     *
     * @param context
     */
    public static String getAndsetLanguage(Context context, String tag) {
        //获取设置的语言 默认是中文
        mLanguage = SharedPrefsUtil.getValue(context, Constants.LANGUAGE, "language", "");
        LogUtils.i(tag + " ： getAndsetLanguage " + mLanguage);
        if (!"".equals(mLanguage)) {
            if (mLanguage.equals(Constants.ZH)) {
                //中文
                changeAppLanguage(context, Locale.CHINA);
            } else if (mLanguage.equals(Constants.TW)) {
                //台湾繁体
                changeAppLanguage(context, Locale.TAIWAN);
            } else if (mLanguage.equals(Constants.EN)) {
                //英文
                changeAppLanguage(context, Locale.US);
            } else if (mLanguage.equals(Constants.DE)) {
                //德语
                changeAppLanguage(context, Locale.GERMANY);
            } else if (mLanguage.equals(Constants.ES)) {
                //西班牙
                Locale Locale_Spain = new Locale("ES", "es", "");
                changeAppLanguage(context, Locale_Spain);
            } else if (mLanguage.equals(Constants.FR)) {
                //法语
                changeAppLanguage(context, Locale.FRENCH);
            } else if (mLanguage.equals(Constants.IT)) {
                //意大利语
                changeAppLanguage(context, Locale.ITALY);
            } else if (mLanguage.equals(Constants.JA)) {
                //日语
                changeAppLanguage(context, Locale.JAPAN);
            } else if (mLanguage.equals(Constants.PL)) {
                //波兰语
                Locale Locale_Polish = new Locale("PL", "pl", "");
                changeAppLanguage(context, Locale_Polish);
            } else if (mLanguage.equals(Constants.PT)) {
                //葡萄牙语
                Locale Locale_Portugal = new Locale("PT", "pt", "");
                changeAppLanguage(context, Locale_Portugal);
            } else if (mLanguage.equals(Constants.RU)) {
                //俄语
                Locale Locale_Russia = new Locale("RU", "ru", "");
                changeAppLanguage(context, Locale_Russia);
            } else if (mLanguage.equals(Constants.KO)) {
                //韩语
                changeAppLanguage(context, Locale.KOREAN);
            } else if (mLanguage.equals(Constants.HI)) {
                //印地语
                Locale Locale_Russia = new Locale("HI", "hi", "");
                changeAppLanguage(context, Locale_Russia);
            } else if (mLanguage.equals(Constants.IN)) {
                //印尼语
                Locale Locale_Russia = new Locale("IN", "in", "");
                changeAppLanguage(context, Locale_Russia);
            } else if (mLanguage.equals(Constants.TH)) {
                //泰语
                Locale Locale_Russia = new Locale("TH", "th", "");
                changeAppLanguage(context, Locale_Russia);
            } else if (mLanguage.equals(Constants.MS)) {
                //马来语
                Locale Locale_Russia = new Locale("MS", "ms", "");
                changeAppLanguage(context, Locale_Russia);
            } else if (mLanguage.equals(Constants.VI)) {
                //越南语
                Locale Locale_Russia = new Locale("VI", "vi", "");
                changeAppLanguage(context, Locale_Russia);
            } else if (mLanguage.equals(Constants.IW)) {
                //西伯来语
                Locale locale = new Locale("IW", "iw", "");
                changeAppLanguage(context, locale);
            } else if (mLanguage.equals(Constants.AR)) {
                //阿拉伯语
                Locale locale = new Locale("AR", "EG", "");
                changeAppLanguage(context, locale);
            } else {
                LogUtils.i("系统语言 11111");
                mLanguage = followSystemSetLanguage(context);
            }
        } else {
            mLanguage = followSystemSetLanguage(context);
            LogUtils.i("系统语言 ggggggggggg " + mLanguage);
        }
        return mLanguage;
    }

    public static String getSystemLang(Context context) {
        String systemLanguage = getSystemLanguage(context).toUpperCase();
        LogUtils.i("系统语言 getSystemLang : " + systemLanguage);
        if ("ZH-CN".equals(systemLanguage)) {
            mLanguage = Constants.ZH;
            //中文
        } else if ("ZH-TW".equals(systemLanguage)) {
            //台湾繁体
            mLanguage = Constants.TW;
        } else if ("EN-US".equals(systemLanguage)) {
            mLanguage = Constants.EN;
            //英文
        } else if ("DE-DE".equals(systemLanguage)) {
            //德语
            mLanguage = Constants.DE;
        } else if ("FR-FR".equals(systemLanguage)) {
            //法语
            mLanguage = Constants.FR;
        } else if ("JA-JP".equals(systemLanguage)) {
            //日语
            mLanguage = Constants.JA;
        } else if ("ES-ES".equals(systemLanguage)) {
            //西班牙
            mLanguage = Constants.ES;
        } else if ("IT-IT".equals(systemLanguage)) {
            //意大利
            mLanguage = Constants.IT;
        } else if ("PL-PL".equals(systemLanguage)) {
            //波兰
            mLanguage = Constants.PL;
        } else if ("PT-PT".equals(systemLanguage)) {
            //葡萄牙
            mLanguage = Constants.PT;
        } else if ("RU-RU".equals(systemLanguage)) {
            //俄语
            mLanguage = Constants.RU;
        } else if ("KO-KR".equals(systemLanguage)) {
            //韩语
            mLanguage = Constants.KO;
        } else if ("HI-IN".equals(systemLanguage)) {
            //印地语
            mLanguage = Constants.HI;
        } else if ("IN-ID".equals(systemLanguage)) {
            //印尼语
            mLanguage = Constants.IN;
        } else if ("TH-TH".equals(systemLanguage)) {
            //泰语
            mLanguage = Constants.TH;
        } else if ("MS-MY".equals(systemLanguage)) {
            //马来语
            mLanguage = Constants.MS;
        } else if ("VI-VN".equals(systemLanguage)) {
            //越南语
            mLanguage = Constants.VI;
        } else if ("IW-IL".equals(systemLanguage)) {
            //西伯来语
            mLanguage = Constants.IW;
        } else if ("AR-EG".equals(systemLanguage)) {
            //阿拉伯语
            mLanguage = Constants.AR;
        } else {
            //英文
            mLanguage = Constants.EN;
        }
        return mLanguage;
    }

    private static String followSystemSetLanguage(Context context) {
        String systemLanguage = getSystemLanguage(context).toUpperCase();
        LogUtils.i(systemLanguage + " : 000--222222222222222--> " + mLanguage);
        if ("ZH-CN".equals(systemLanguage)) {
            mLanguage = Constants.ZH;
            //中文
            changeAppLanguage(context, Locale.SIMPLIFIED_CHINESE);
        } else if ("ZH-TW".equals(systemLanguage)) {
            //台湾繁体
            mLanguage = Constants.TW;
            changeAppLanguage(context, Locale.TAIWAN);
        } else if ("EN-US".equals(systemLanguage)) {
            mLanguage = Constants.EN;
            //英文
            changeAppLanguage(context, Locale.US);
        } else if ("DE-DE".equals(systemLanguage)) {
            //德语
            mLanguage = Constants.DE;
            changeAppLanguage(context, Locale.GERMANY);
        } else if ("FR-FR".equals(systemLanguage)) {
            //法语
            mLanguage = Constants.FR;
            changeAppLanguage(context, Locale.FRENCH);
        } else if ("JA-JP".equals(systemLanguage)) {
            //日语
            mLanguage = Constants.JA;
        } else if ("ES-ES".equals(systemLanguage)) {
            //西班牙
            mLanguage = Constants.ES;
            Locale Locale_Spain = new Locale("ES", "es", "");
            changeAppLanguage(context, Locale_Spain);
        } else if ("IT-IT".equals(systemLanguage)) {
            //意大利
            mLanguage = Constants.IT;
            changeAppLanguage(context, Locale.ITALY);
        } else if ("PL-PL".equals(systemLanguage)) {
            //波兰
            mLanguage = Constants.PL;
            Locale Locale_Polish = new Locale("PL", "pl", "");
            changeAppLanguage(context, Locale_Polish);
        } else if ("PT-PT".equals(systemLanguage)) {
            //葡萄牙
            mLanguage = Constants.PT;
            Locale Locale_Portugal = new Locale("PT", "pt", "");
            changeAppLanguage(context, Locale_Portugal);
        } else if ("RU-RU".equals(systemLanguage)) {
            //俄语
            mLanguage = Constants.RU;
            Locale locale = new Locale("RU", "ru", "");
            changeAppLanguage(context, locale);
        } else if (systemLanguage.equals("KO-KR")) {
            //韩语
            mLanguage = Constants.KO;
            changeAppLanguage(context, Locale.KOREAN);
        } else if (systemLanguage.equals("HI-IN")) {
            //印地语
            mLanguage = Constants.HI;
            Locale locale = new Locale("HI", "hi", "");
            changeAppLanguage(context, locale);
        } else if (systemLanguage.equals("IN-ID")) {
            //印尼语
            mLanguage = Constants.IN;
            Locale locale = new Locale("IN", "in", "");
            changeAppLanguage(context, locale);
        } else if (systemLanguage.equals("TH-TH")) {
            //泰语
            mLanguage = Constants.TH;
            Locale locale = new Locale("TH", "th", "");
            changeAppLanguage(context, locale);
        } else if (systemLanguage.equals("MS-MY")) {
            //马来语
            mLanguage = Constants.MS;
            Locale locale = new Locale("MS", "ms", "");
            changeAppLanguage(context, locale);
        } else if (systemLanguage.equals("VI-VN")) {
            //越南语
            mLanguage = Constants.VI;
            Locale locale = new Locale("VI", "vi", "");
            changeAppLanguage(context, locale);
        } else if (systemLanguage.equals("IW-IL")) {
            //希伯来语
            mLanguage = Constants.IW;
            // Log.i(TAG, " 设置 希伯来语--> ");
            Locale locale = new Locale("IW", "iw", "");
            changeAppLanguage(context, locale);
        } else if (systemLanguage.equals("AR-EG")) {
            //阿拉伯语
           // Log.i(TAG, " 设置 阿拉伯语--> ");
            Locale locale = new Locale("AR", "EG", "");
            changeAppLanguage(context, locale);
        } else {
            mLanguage = Constants.EN;
            //英文
            changeAppLanguage(context, Locale.US);
        }
        return mLanguage;
    }

}
